package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.OrganismSpecimen;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QOrganismSpecimen;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = OrganismSpecimenRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = OrganismSpecimenRepositoryCustom.class)
@Transactional(readOnly = true)
public class OrganismSpecimenRepositoryImpl extends QueryDslRepositorySupportExt<OrganismSpecimen> implements OrganismSpecimenRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    OrganismSpecimenRepositoryImpl() {
        super(OrganismSpecimen.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_PERSON = "collectPerson";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String STATUS = "status";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRESERVE_LOCATION = "preserveLocation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_COORDINATE = "collectCoordinate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_DATE = "collectDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_LOCATION = "collectLocation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_NUMBER = "collectNumber";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TYPE = "type";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<OrganismSpecimen> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QOrganismSpecimen organismSpecimen = QOrganismSpecimen.organismSpecimen;
        JPQLQuery<OrganismSpecimen> query = from(organismSpecimen);
        Assert.notNull(organism, "organism is required");
        query.where(organismSpecimen.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { organismSpecimen.organism, organismSpecimen.collectNumber, organismSpecimen.collectLocation, organismSpecimen.collectDate, organismSpecimen.collectPerson, organismSpecimen.collectCoordinate, organismSpecimen.status, organismSpecimen.description, organismSpecimen.preserveLocation, organismSpecimen.type};
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, organismSpecimen.organism).map(COLLECT_NUMBER, organismSpecimen.collectNumber).map(COLLECT_LOCATION, organismSpecimen.collectLocation).map(COLLECT_DATE, organismSpecimen.collectDate).map(COLLECT_PERSON, organismSpecimen.collectPerson).map(COLLECT_COORDINATE, organismSpecimen.collectCoordinate).map(STATUS, organismSpecimen.status).map(DESCRIPTION, organismSpecimen.description).map(PRESERVE_LOCATION, organismSpecimen.preserveLocation).map( TYPE, organismSpecimen.type );
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organismSpecimen);
    }
}
